package com.shir.travels.ui.tuisong;

import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.shir.travels.R;

import java.util.List;

public class TuisongRecyclerAdapter extends RecyclerView.Adapter<TuisongRecyclerAdapter.MyViewHolder> implements View.OnClickListener {

    private List<Integer> imageResources;
    private List<String> comments;

    public TuisongRecyclerAdapter(List<Integer> imageResources, List<String> comments) {
        this.imageResources = imageResources;
        this.comments = comments;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tuisong,parent,false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.imageView.setImageResource(imageResources.get(position));
        holder.textView.setText(Html.fromHtml(comments.get(position), Html.FROM_HTML_MODE_LEGACY));
        holder.itemView.setTag(position);
        holder.imageView.setTag(position);
        holder.textView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return imageResources.size();
    }

    /**
     * references to views
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView textView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = (ImageView)itemView.findViewById(R.id.imageView33);
            textView = (TextView)itemView.findViewById(R.id.textView37);

            itemView.setOnClickListener(TuisongRecyclerAdapter.this);
            imageView.setOnClickListener(TuisongRecyclerAdapter.this);
            textView.setOnClickListener(TuisongRecyclerAdapter.this);
        }
    }

    public interface OnItemClickListener{
        void onClick(View view, int position);
    }

    private OnItemClickListener itemClickListener=null;

    public void setOnItemClickListener(OnItemClickListener listener){
        this.itemClickListener=listener;
    }

    @Override
    public void onClick(View view) {
        if(itemClickListener!=null){
            int position = (int)view.getTag();
            itemClickListener.onClick(view, position);
        }
    }
}
